package com.chuangke.metadata.controller;

import com.chuangke.metadata.entity.SysBill;
import com.chuangke.metadata.entity.SysBillSerial;
import com.chuangke.metadata.service.SysBillSerialService;
import com.chuangke.metadata.service.SysBillService;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.http.HttpResult;
import com.chuangke.common.idgen.IdManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author chuangke
 * @date "2024-01-09"
 */
@RestController
@RequestMapping("/sys/bill")
public class SysBillController {

	@Autowired
	private SysBillService sysBillService;

	@Autowired
	private SysBillSerialService sysBillSerialService;

	/**
	 * 查询字典类型列表
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/list")
	public HttpResult list(String name) {
		Map<String, Object> params = new HashMap<>();
		params.put("name", name);
		CkPageQuery query = new CkPageQuery(params);

		return HttpResult.ok(sysBillService.page(query));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/listAll")
	public HttpResult listAll() {
		return HttpResult.ok(sysBillService.findList());
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/loadByBillType")
	public HttpResult loadByBillType(String billType) {
		return HttpResult.ok(sysBillService.loadByBillType(billType));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/page")
	public HttpResult page(@RequestBody Map<String, Object> params) {
		return HttpResult.ok(sysBillService.page(new CkPageQuery(params)));
	}

	/**
	 * 获取字典类型详细信息
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/{id}")
	public HttpResult getInfo(@PathVariable("id") String id) {
		return HttpResult.ok(sysBillService.findById(id));
	}

	/**
	 * 新增字典类型
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping
	public HttpResult save(@RequestBody SysBill action) {
		if(StringUtils.isEmpty(action.getId())){
			action.setId(IdManager.nextId());

		}
		sysBillService.save(action);
		return HttpResult.ok();
	}

	/**
	 * 删除字典类型
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@DeleteMapping("/{ids}")
	public HttpResult remove(@PathVariable String[] ids) {
		sysBillService.deleteByIds(Arrays.asList(ids));
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/saveDetail")
	public HttpResult saveDetail(@RequestBody SysBillSerial record) {
		sysBillSerialService.save(record);
		return HttpResult.ok() ;
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/delDetailByIds")
	public HttpResult delDetailByIds(@RequestBody List<String> ids) {
		sysBillSerialService.remove(ids);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/loadByBillKey")
	public HttpResult loadByBillKey(@RequestBody SysBillSerial record) {
		return HttpResult.ok(sysBillSerialService.list(record.getBillKey()));
	}
}
